<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of cms
 * 
 * @author Joydeb Choudhury (mcajoydeb@gmail.com)
 */
class News extends MY_Controller {

    function News() {
        parent :: __construct();
        $this->load->model('news_model');
        $this->load->model('counter_model');
        $this->load->library('pagination');
        $this->template->set_theme('admin');
        $this->template->set_layout('admin_layout');
        $this->commoncall->check_admin_login();
    }
//----------Listing of news (26.9.12)-----------
    function index() {
        $offset = 0 + $this->uri->segment(4);
        $config['base_url'] = base_url() . 'admin/news/index';
        $total = $this->counter_model->count_all_record('news');
        $perpage = 5;
        $config['per_page'] = $perpage;
        $config['total_rows'] = $total;
        $config['uri_segment'] = '4';
        $data['news'] = $this->news_model->get_all_news($perpage, $offset);
        $this->pagination->initialize($config);
        $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_news', $data);
    }
//----------Submit news (26.9.12)-----------
    function submit_news() { {
            $this->form_validation->set_message('required', '* Required');
            $this->form_validation->set_rules('title', 'Required', 'trim|required||is_unique[news.title]');
            $this->form_validation->set_rules('meta_title', 'Required', 'trim|required');
            $this->form_validation->set_rules('meta_keyword', 'Required', 'trim|required');
            $this->form_validation->set_rules('meta_description', 'Required', 'trim|required');
            $this->form_validation->set_rules('news_detail', 'Required', 'trim|required');
            if ($this->form_validation->run() == FALSE) {
                $this->session->set_flashdata('message', 'Error ! Invalid Field data ...');
                redirect('admin/news/');
            } else {
                $post_data = array(
                    'title' => $this->input->post('title'),
                    'meta_title' => $this->input->post('meta_title'),
                    'meta_keyword' => $this->input->post('meta_keyword'),
                    'meta_description' => $this->input->post('meta_description'),
                    'description' => $this->input->post('news_detail'),
                    'date' => date('d-M-Y', now()),
                    'status' => 51
                );
                $this->db->insert('news', $post_data);
                $this->session->set_flashdata('message', 'Added successfully..');
                redirect('admin/news/');
            }
        }
    }
//----------Edit news form display (26.9.12)-----------
    function edit_news($id) {

        $data['newsrow']['info'] = $this->news_model->get_news($id);
        $this->template->title('Dashboard', $this->settings_model->get_setting('webtitle'))
                ->set_partial('admin_header', 'header/admin_header')
                ->set_partial('admin_left_menu', 'sidebar/admin_left_menu')
                ->build('pages/admin_edit_news', $data);
    }
//----------Edit news submition(26.9.12)-----------
    function edit_submit() {
        $this->form_validation->set_message('required', '* Required');
        $this->form_validation->set_rules('news_id', 'Required', 'trim|required|');
        $this->form_validation->set_rules('title', 'Required', 'trim|required|');
        $this->form_validation->set_rules('meta_title', 'Required', 'trim');
        $this->form_validation->set_rules('meta_keyword', 'Required', 'trim');
        $this->form_validation->set_rules('meta_description', 'Required', 'trim');
        $this->form_validation->set_rules('news_detail', 'Required', 'trim|required');
        $id = $this->input->post('news_id');
        if ($this->form_validation->run() == FALSE) {
            $this->session->set_flashdata('message', 'Error ! Invalid Field data ...');
            redirect('admin/news/');
        } else {
            $post_data = array(
                'title' => $this->input->post('title'),
                'meta_title' => $this->input->post('meta_title'),
                'meta_keyword' => $this->input->post('meta_keyword'),
                'meta_description' => $this->input->post('meta_description'),
                'description' => $this->input->post('news_detail'),
                'updated_on' => date('d-M-Y', now()),
            );
            $this->db->where('id', $id);
            $this->db->update('news', $post_data);
            $this->session->set_flashdata('message', 'Updated successfully..');
            redirect('admin/news/');
        }
    }
//--------Delete News by Id-----------------------    
    function delete_news($id) {
        if ($id) {
            $sql = "delete from news where id=$id";
            $this->db->query($sql);
            redirect('admin/news/');
        }
    }
}
?>